home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / OCFSRC.PAK / OCSTORAG.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  12KB  |  590 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectComponents
  3. // Copyright (c) 1994, 1997 by Borland International, All Rights Reserved
  4. //
  5. // $Revision:   2.7  $
  6. //
  7. //   Implementation of TOcStorage & TOcStream IStorage/IStream encapsulation
  8. //----------------------------------------------------------------------------
  9. #include <ocf/pch.h>
  10. #if !defined(OCF_OCOBJECT_H)
  11. # include <ocf/ocobject.h>
  12. #endif
  13. #if !defined(OCF_OCSTORAG_H)
  14. # include <ocf/ocstorag.h>
  15. #endif
  16. #if !defined(OCF_OCBOCOLE_H)
  17. # include <ocf/ocbocole.h>
  18. #endif
  19. #if !defined(WINSYS_GEOMETRY_H)
  20. # include <winsys/geometry.h>
  21. #endif
  22. #include <iostream.h>
  23.  
  24. DIAG_DEFINE_GROUP(OcExcept, true, 1);
  25.  
  26. // Simple refcount debug assistant
  27. //
  28. #if defined(CHECK_REFCOUNT)
  29. static void RefCountCheck(IStorage far* si) {
  30.   uint32 count = si->AddRef();
  31.   count = si->Release();
  32. }
  33. #else
  34. # define RefCountCheck(si)
  35. #endif
  36.  
  37. //
  38. // Storage sharing mode bit mask
  39. //
  40. #define STGM_SHARE_MASK    (STGM_SHARE_DENY_NONE | STGM_SHARE_DENY_READ| \
  41.                             STGM_SHARE_DENY_WRITE | STGM_SHARE_EXCLUSIVE)
  42.  
  43. //----------------------------------------------------------------------------
  44. // TXObjComp
  45.  
  46.  
  47. TXObjComp::~TXObjComp()
  48. {
  49. }
  50.  
  51. TXObjComp* TXObjComp::Clone()
  52. {
  53.   return new TXObjComp(*this);
  54. }
  55.  
  56. void TXObjComp::Throw()
  57. {
  58.   THROW( *this );
  59. }
  60.  
  61. void TXObjComp::Check(HRESULT stat, TError err, const char far* arg)
  62. {
  63.   if (FAILED(stat))
  64.     Throw(err, stat, arg);
  65. }
  66.  
  67. static char* sObjCompErrMsgs[] = {
  68.   "",                                           // xNoError
  69.   "Could not locate " BOLEDLL,                  // xBOleLoadFail
  70.   "Incompatible version of " BOLEDLL,           // xBOleVersFail
  71.   "Could not obtain BOle ClassMgr",             // xBOleBindFail
  72.   "Document factory [un]registration failed",   // xDocFactoryFail
  73.   "Missing Root IStorage in OcDocument",        // xMissingRootIStorage
  74.   "Internal OcPart creation error",             // xInternalPartError
  75.   "OcPart initialization failure",              // xPartInitError
  76.   "Unable to open/create RootStorage on '%s'",  // xRootStorageOpenError
  77.   "Unable to open/create Storage '%s'",         // xStorageOpenError
  78.   "Unable to open/create Storage on ILockBytes",// xStorageILockError
  79.   "Unable to open/create Stream '%s'"           // xStreamOpenError
  80. };
  81.  
  82. void TXObjComp::Throw(TError err, HRESULT hr, const char far* arg)
  83. {
  84.   if (!InstanceCount) {
  85.     char buf[128+1];
  86.     wsprintf(buf, sObjCompErrMsgs[err], arg);
  87.  
  88.     if (hr != HR_FAIL) {  // generic error, dont bother with message.
  89.       strcat(buf, ": ");
  90.       int len = strlen(buf);
  91.       OleErrorFromCode(hr, buf + len, sizeof buf - len - 2);
  92.     }
  93.     strcat(buf, ".");
  94.  
  95.     WARNX(OcExcept, hr != HR_NOERROR, 0, buf);
  96.     throw TXObjComp(err, buf, hr);
  97.   }
  98. }
  99.  
  100. //----------------------------------------------------------------------------
  101. // TOcStream
  102.  
  103. //
  104. //
  105. //
  106. TOcStream::TOcStream(TOcStorage& storage, const char far* name, bool create, uint32 mode)
  107. {
  108.   // Make sure that the transacted mode is off since streams don't support
  109.   // this mode. Also make sure that the stream is opened in exclusive mode.
  110.   //
  111.   mode &= ~STGM_TRANSACTED;
  112.   mode = (mode & ~STGM_SHARE_MASK) | STGM_SHARE_EXCLUSIVE;
  113.  
  114.   HRESULT hr = storage.OpenStream(name, 0, mode, 0, &StreamI);
  115.   if (!StreamI && create)
  116.     hr = storage.CreateStream(name, mode, 0, 0, &StreamI);
  117.   TXObjComp::Check(hr, TXObjComp::xStreamOpenError, name);
  118. }
  119.  
  120. //
  121. //
  122. //
  123. TOcStream::TOcStream(TOcStream& stream)
  124. {
  125.   stream.Clone(&StreamI);
  126.   // if (!StreamI) throw...?
  127. }
  128.  
  129. //
  130. //
  131. //
  132. TOcStream::TOcStream(IStream* stream)
  133. :
  134.   StreamI(stream)
  135. {
  136. }
  137.  
  138. //
  139. //
  140. //
  141. TOcStream::~TOcStream()
  142. {
  143.   if (StreamI)
  144.     StreamI->Release();
  145. }
  146.  
  147. //
  148. //
  149. //
  150. IStream*
  151. TOcStream::GetIStream()
  152. {
  153.   return StreamI;
  154. }
  155.  
  156. //
  157. //
  158. //
  159. HRESULT
  160. TOcStream::Read(void HUGE* pv, ulong cb, ulong far* read)
  161. {
  162.   PRECONDITION(pv && read && StreamI);
  163.  
  164.   return StreamI->Read(pv, cb, read);
  165. }
  166.  
  167. //
  168. //
  169. //
  170. HRESULT
  171. TOcStream::Write(void const HUGE* pv, ulong cb, ulong far* written)
  172. {
  173.   PRECONDITION(pv && written && StreamI);
  174.  
  175.   return StreamI->Write(pv, cb, written);
  176. }
  177.  
  178. //
  179. //
  180. //
  181. HRESULT
  182. TOcStream::Seek(int64 move, uint32 origin, uint64 far* newPosition)
  183. {
  184.   PRECONDITION(newPosition && StreamI);
  185.  
  186.   return StreamI->Seek(move, origin, (ULARGE_INTEGER*)newPosition);
  187. }
  188.  
  189. //
  190. //
  191. //
  192. HRESULT
  193. TOcStream::SetSize(uint64 newSize)
  194. {
  195.   return StreamI->SetSize(newSize);
  196. }
  197.  
  198. //
  199. //
  200. //
  201. HRESULT
  202. TOcStream::CopyTo(TOcStream& stream, uint64 cb, uint64 far* read, uint64 far* written)
  203. {
  204.   PRECONDITION(read);
  205.  
  206.   return StreamI->CopyTo(stream.GetIStream(), cb, (ULARGE_INTEGER*)read,
  207.                          (ULARGE_INTEGER*)written);
  208. }
  209.  
  210. //
  211. //
  212. //
  213. HRESULT
  214. TOcStream::Commit(uint32 commitFlags)
  215. {
  216.   return StreamI->Commit(commitFlags);
  217. }
  218.  
  219. //
  220. //
  221. //
  222. HRESULT
  223. TOcStream::Revert()
  224. {
  225.   return StreamI->Revert();
  226. }
  227.  
  228. //
  229. //
  230. //
  231. HRESULT
  232. TOcStream::LockRegion(uint64 offset, uint64 cb, uint32 lockType)
  233. {
  234.   return StreamI->LockRegion(offset, cb, lockType);
  235. }
  236.  
  237. //
  238. //
  239. //
  240. HRESULT
  241. TOcStream::UnlockRegion(uint64 offset, uint64 cb, uint32 lockType)
  242. {
  243.   return StreamI->UnlockRegion(offset, cb, lockType);
  244. }
  245.  
  246. //
  247. //
  248. //
  249. HRESULT
  250. TOcStream::Stat(STATSTG far* statstg, uint32 statFlag)
  251. {
  252.   PRECONDITION(statstg);
  253.  
  254.   return StreamI->Stat(statstg, statFlag);
  255. }
  256.  
  257. //
  258. //
  259. //
  260. HRESULT
  261. TOcStream::Clone(IStream far* far* stream)
  262. {
  263.   PRECONDITION(stream);
  264.  
  265.   return StreamI->Clone(stream);
  266. }
  267.  
  268. //----------------------------------------------------------------------------
  269. // TOcStorage
  270.  
  271. //
  272. //
  273. //
  274. TOcStorage::TOcStorage(const char far* fileName, bool create, uint32 mode)
  275. {
  276. //  Parent = 0;
  277.  
  278.   // Fill in the sharing mode based on the access
  279.   //
  280.   if ((mode & STGM_WRITE) || (mode & STGM_READWRITE))
  281.     mode = (mode & ~STGM_SHARE_MASK) | STGM_SHARE_DENY_WRITE;
  282.   else
  283.     mode = (mode & ~STGM_SHARE_MASK) | STGM_SHARE_DENY_NONE;
  284.  
  285.   HRESULT hr;
  286.   if (create) {
  287.     mode |= STGM_CREATE;
  288.     if (!fileName)
  289.       mode |= STGM_DELETEONRELEASE;
  290.     hr = ::StgCreateDocfile(OleStr(fileName), mode, 0, &StorageI);
  291.   }
  292.   else {
  293.     hr = ::StgOpenStorage(OleStr(fileName), 0, mode, 0, 0, &StorageI);
  294.   }
  295.   RefCountCheck(StorageI);
  296.   TXObjComp::Check(hr, TXObjComp::xRootStorageOpenError, fileName);
  297. }
  298.  
  299. //
  300. //
  301. //
  302. TOcStorage::TOcStorage(ILockBytes far* lkbyt, bool create, uint32 mode)
  303. {
  304.   PRECONDITION(lkbyt);
  305.  
  306.   // Fill in the sharing mode based on the access
  307.   //
  308.   if ((mode & STGM_WRITE) || (mode & STGM_READWRITE))
  309.     mode = (mode & ~STGM_SHARE_MASK) | STGM_SHARE_DENY_WRITE;
  310.   else
  311.     mode = (mode & ~STGM_SHARE_MASK) | STGM_SHARE_DENY_NONE;
  312.  
  313.   HRESULT hr;
  314.   if (create) {
  315.     mode |= STGM_CREATE;
  316.     hr = ::StgCreateDocfileOnILockBytes(lkbyt, mode, 0, &StorageI);
  317.   }
  318.   else {
  319.     hr = ::StgOpenStorageOnILockBytes(lkbyt, 0,  // IStorage* priority???
  320.              mode, 0, 0, &StorageI);
  321.   }
  322.   RefCountCheck(StorageI);
  323.   TXObjComp::Check(hr, TXObjComp::xStorageILockError);
  324. }
  325.  
  326. //
  327. //
  328. //
  329. TOcStorage::TOcStorage(TOcStorage& parent, const char far* name, bool create, uint32 mode)
  330. {
  331. //  Parent = &parent;
  332.   mode = (mode & ~STGM_SHARE_MASK) | STGM_SHARE_EXCLUSIVE;
  333.   HRESULT hr;
  334.   hr = parent.OpenStorage(name, 0, mode, 0, 0, &StorageI);
  335.   if (!StorageI && create)
  336.     hr = parent.CreateStorage(name, mode, 0, 0, &StorageI);
  337.  
  338.   RefCountCheck(StorageI);
  339.   TXObjComp::Check(hr, TXObjComp::xStorageOpenError, name);
  340. }
  341.  
  342. //
  343. //
  344. //
  345. TOcStorage::TOcStorage(IStorage* storage)
  346. :
  347.   StorageI(storage)
  348. {
  349.   if (StorageI) {
  350.     StorageI->AddRef();
  351.     RefCountCheck(StorageI);
  352.   }
  353. }
  354.  
  355. //
  356. //
  357. //
  358. TOcStorage::~TOcStorage()
  359. {
  360.   if (StorageI) {
  361.     RefCountCheck(StorageI);
  362.     StorageI->Release();
  363.   }
  364. }
  365.  
  366. //
  367. //
  368. //
  369. ulong
  370. TOcStorage::AddRef()
  371. {
  372.   return StorageI? StorageI->AddRef() : 0;
  373. }
  374.  
  375. //
  376. //
  377. //
  378. ulong
  379. TOcStorage::Release()
  380. {
  381.   return StorageI? StorageI->Release() : 0;
  382. }
  383.  
  384. //
  385. //
  386. //
  387. IStorage*
  388. TOcStorage::GetIStorage()
  389. {
  390.   return StorageI;
  391. }
  392.  
  393. //
  394. //
  395. //
  396. HRESULT
  397. TOcStorage::CopyTo(uint32 ciidExclude, IID const far* rgiidExclude, SNB snbExclude, TOcStorage& dest)
  398. {
  399.   return StorageI->CopyTo(ciidExclude, rgiidExclude, snbExclude, dest.GetIStorage());
  400. }
  401.  
  402. //
  403. //
  404. //
  405. HRESULT
  406. TOcStorage::MoveElementTo(char const far* name, TOcStorage& dest, char const far* newName, uint32 flags)
  407. {
  408.   return StorageI->MoveElementTo(OleStr(name), dest.GetIStorage(), OleStr(newName), flags);
  409. }
  410.  
  411. //
  412. //
  413. //
  414. HRESULT
  415. TOcStorage::Commit(uint32 commitFlags)
  416. {
  417.   return StorageI->Commit(commitFlags);
  418. }
  419.  
  420. //
  421. //
  422. //
  423. HRESULT
  424. TOcStorage::Revert()
  425. {
  426.   return StorageI->Revert();
  427. }
  428.  
  429. //
  430. //
  431. //
  432. HRESULT
  433. TOcStorage::EnumElements(uint32 rsrvd1, void far* rsrvd2, uint32 rsrvd3, IEnumSTATSTG far*far* enm)
  434. {
  435.   return StorageI->EnumElements(rsrvd1, rsrvd2, rsrvd3, enm);
  436. }
  437.  
  438. //
  439. //
  440. //
  441. HRESULT
  442. TOcStorage::DestroyElement(const char far* name)
  443. {
  444.   PRECONDITION(name);
  445.  
  446.   return StorageI->DestroyElement(OleStr(name));
  447. }
  448.  
  449. //
  450. //
  451. //
  452. HRESULT
  453. TOcStorage::RenameElement(const char far* oldName, const char far* newName)
  454. {
  455.   return StorageI->RenameElement(OleStr(oldName), OleStr(newName));
  456. }
  457.  
  458. //
  459. //
  460. //
  461. HRESULT
  462. TOcStorage::SetElementTimes(const char far* name, FILETIME const far* ctime, FILETIME const far* atime, FILETIME const far* mtime)
  463. {
  464.   return StorageI->SetElementTimes(OleStr(name), ctime, atime, mtime);
  465. }
  466.  
  467. //
  468. //
  469. //
  470. HRESULT
  471. TOcStorage::SetClass(const IID far& clsid)
  472. {
  473.   return StorageI->SetClass(clsid);
  474. }
  475.  
  476. //
  477. //
  478. //
  479. HRESULT
  480. TOcStorage::SetStateBits(uint32 stateBits, uint32 mask)
  481. {
  482.   return StorageI->SetStateBits(stateBits, mask);
  483. }
  484.  
  485. //
  486. //
  487. //
  488. HRESULT
  489. TOcStorage::Stat(STATSTG far* statstg, uint32 statFlag)
  490. {
  491.   PRECONDITION(statstg);
  492.  
  493.   return StorageI->Stat(statstg, statFlag);
  494. }
  495.  
  496. //
  497. //
  498. //
  499. HRESULT
  500. TOcStorage::SwitchToFile(const char far* newPath)
  501. {
  502.   if (!newPath)
  503.     return HR_INVALIDARG;
  504.  
  505.   IRootStorage* rootStorageI;
  506.   HRESULT hr = StorageI->QueryInterface(IID_IRootStorage, &(void far*)rootStorageI);
  507.   if (HRSucceeded(hr)) {
  508.     hr = rootStorageI->SwitchToFile(OleStr(const_cast<char far*>(newPath)));
  509.     rootStorageI->Release();
  510.   }
  511.   return hr;
  512. }
  513.  
  514. //
  515. //
  516. //
  517. HRESULT
  518. TOcStorage::CreateStream(const char far* name, uint32 mode, uint32 rsrvd1, uint32 rsrvd2, IStream far* far* stream)
  519. {
  520.   PRECONDITION(name);
  521.  
  522.   return StorageI->CreateStream(OleStr(name), mode, rsrvd1, rsrvd2, stream);
  523. }
  524.  
  525. //
  526. //
  527. //
  528. HRESULT
  529. TOcStorage::OpenStream(const char far* name, void far *rsrvd1, uint32 mode, uint32 rsrvd2, IStream far*far* stream)
  530. {
  531.   PRECONDITION(name);
  532.  
  533.   return StorageI->OpenStream(OleStr(name), rsrvd1, mode, rsrvd2, stream);
  534. }
  535.  
  536. //
  537. //
  538. //
  539. HRESULT
  540. TOcStorage::CreateStorage(const char far* name, uint32 mode, uint32 rsrvd1, uint32 rsrvd2, IStorage far*far* storage)
  541. {
  542.   PRECONDITION(StorageI && name);
  543.  
  544.   return StorageI->CreateStorage(OleStr(name), mode, rsrvd1, rsrvd2, storage);
  545. }
  546.  
  547. //
  548. //
  549. //
  550. HRESULT
  551. TOcStorage::OpenStorage(const char far* name, IStorage far* stgPriority, uint32 mode, SNB snbExclude, uint32 rsrvd, IStorage far*far* storage)
  552. {
  553.   PRECONDITION(name);
  554.  
  555.   return StorageI->OpenStorage(OleStr(name), stgPriority, mode, snbExclude, rsrvd, storage);
  556. }
  557.  
  558. //
  559. //
  560. //
  561. HRESULT
  562. TOcStorage::IsStorageFile(const char far* name)
  563. {
  564.   PRECONDITION(name);
  565.  
  566.   return ::StgIsStorageFile(OleStr(name));
  567. }
  568.  
  569. //
  570. //
  571. //
  572. HRESULT
  573. TOcStorage::IsStorageILockBytes(ILockBytes far* lkbyt)
  574. {
  575.   PRECONDITION(lkbyt);
  576.  
  577.   return ::StgIsStorageILockBytes(lkbyt);
  578. }
  579.  
  580. //
  581. //
  582. //
  583. HRESULT
  584. TOcStorage::SetTimes(char const far* name, FILETIME const far* ctime, FILETIME const far* atime, FILETIME const far* mtime)
  585. {
  586.   PRECONDITION(name);
  587.  
  588.   return ::StgSetTimes(OleStr(name), ctime, atime, mtime);
  589. }
  590.